const {
    execSQL
} = require('../db/mysql')

const {
    generateToken,

    decodePassword
} = require('../utils')



//登录
const login = ({
    username,
    password
}) => {
    console.log('验证登录',username,password)

    let sql = `select * from users where username = '${username}'`
    return execSQL(sql).then((res) => {

        if (res[0]) {

            let pwd = decodePassword(res[0].password);
            if(pwd == password){
                let token = generateToken({uid:res[0].id,created:res[0].created})
                return {
                    token,nickname:res[0].nickname,username:res[0].username
                };
            }else{
                return false;
            }
            
        };
        return false;
    }).catch(e => {
        throw e;
        return false;
    })
}


module.exports = {
    login,

}